package com.chengxiaoxiao.ai_demo.config.pgvector;

import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.store.embedding.pgvector.PgVectorEmbeddingStore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import static com.chengxiaoxiao.ai_demo.config.pgvector.PgVectorEmbeddingStoreProperties.PREFIX;


@Configuration
@EnableConfigurationProperties(PgVectorEmbeddingStoreProperties.class)
@ConditionalOnProperty(prefix = PREFIX, name = "enabled", havingValue = "true", matchIfMissing = true)
public class PgVectorEmbeddingStoreAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public PgVectorEmbeddingStore pgvectorEmbeddingStore(PgVectorEmbeddingStoreProperties properties, EmbeddingModel embeddingModel) {
        return PgVectorEmbeddingStore.builder()
                .host(properties.getHost())
                .port(properties.getPort())
                .database(properties.getDatabase())
                .user(properties.getUser())
                .password(properties.getPassword())
                .table(properties.getTableName())
                .dimension(embeddingModel.dimension())
                .createTable(properties.getCreateTable())
                .dropTableFirst(properties.getDropTableFirst())
                .useIndex(properties.getUseIndex())
                .indexListSize(properties.getIndexListSize())
                .metadataStorageConfig(properties.getMetadataStorageConfig())
                .build();
    }
}
